import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

public class Kyssad_vazzad_paneel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	Kysimused kysimuste_database;
	Kysimus selectedQuestion = new Kysimus();
	JLabel kysimus, kysvastlabel, protsent;
	JComboBox teemadList;
	JButton vasta, j2rgmine, nullinupp;
	ArrayList<ArrayList<JCheckBox>> tsekboxid;
	ArrayList<ArrayList<JLabel>> leibelid;
	ArrayList<ArrayList<JPanel>> vastuserida;
	ArrayList<JLabel> pildiNupp;
	boolean menyyNupudTehtud, gamemode;
	int count, kysimustearv, protsentarv, kyspikkus;
	double oiged, vastuse_protsent;
	JPanel vastustepaneel, kysvast, vastapaneel;
	
	public Kyssad_vazzad_paneel(Kysimused kysimuste_data, boolean gamemode) {
		kysimuste_database = kysimuste_data;	
		protsentarv = 0;
		oiged = 0;
		kysimustearv = 0;
		menyyNupudTehtud = false;
		this.gamemode = gamemode;
		setLayout(new BorderLayout());
		setBackground(new Color(191, 239, 255));
	}
	
	public void tekita_listid() {
		int max_vastuste_arv = 10;
		tsekboxid = new ArrayList<ArrayList<JCheckBox>>();
		leibelid = new ArrayList<ArrayList<JLabel>>();
		vastuserida = new ArrayList<ArrayList<JPanel>>();
		
		for (int i = 0; i < max_vastuste_arv; i++) { // teeme defauldina siia 10 vastusevariandi võimaluse
			JPanel uus_kogum = new JPanel();
			uus_kogum.setLayout(new GridLayout(i, 2));
			ArrayList<JCheckBox> uusCheckboxid = new ArrayList<JCheckBox>();
			ArrayList<JLabel> uusJLabelid = new ArrayList<JLabel>();
			ArrayList<JPanel>uusJPanelid = new ArrayList<JPanel>();
			
			for (int j = 0; j < i; j++) {
				JPanel uus_paneel = new JPanel();
				uus_paneel.setLayout(new BorderLayout());
				String kysimus = "<html>" + "Defaultvastuse tekst"+ "</html>";
				JLabel uus_label = new JLabel(kysimus);
				uus_label.setFont(new Font("Arial",1,16)); // (int)(0.025 * this.getWidth())
				uus_label.setOpaque(true);
				uus_label.setBackground(new Color(209, 238, 238));
				uus_label.setBorder(BorderFactory.createLineBorder(Color.cyan));
				uus_paneel.add(uus_label, BorderLayout.CENTER);
				JCheckBox uus_checkbox =  new JCheckBox();
				uus_checkbox.setBackground(new Color(209, 238, 238));
				uus_checkbox.setOpaque(true);
				uus_paneel.add(uus_checkbox, BorderLayout.EAST);
				uusCheckboxid.add(uus_checkbox);
				uusJLabelid.add(uus_label);
				uusJPanelid.add(uus_paneel);
				uus_kogum.add(uus_paneel);
			}
			
			tsekboxid.add(uusCheckboxid);
			leibelid.add(uusJLabelid);
			vastuserida.add(uusJPanelid);
			vastustepaneel.add(uus_kogum, ("Kogum" + i));

		}
		
	}

	public void lae_kysimuselabel() {
		if(kysimus==null)
			kysimus = new JLabel(selectedQuestion .get_kysimus(),JLabel.CENTER);
		this.add(kysimus, BorderLayout.NORTH);
		kysimus.setBorder(BorderFactory.createLineBorder(Color.black));
		kysimus.setBackground(new Color(176, 226, 255));
		kysimus.setOpaque(true);
	}
	
	public void lae_vastustepaneel() {
		vastustepaneel = new JPanel();
		vastustepaneel.setBorder(BorderFactory.createLineBorder(Color.pink));
		vastustepaneel.setLayout(new CardLayout());	
		this.add(vastustepaneel);
	}
	
	public void lae_nupud() {
		vastapaneel = new JPanel();
		vastapaneel.setLayout(new GridLayout(3, 1));
		vastapaneel.add(new JPanel());
		if (!this.gamemode) {
			nullinupp = new JButton("Nulli seis!");
			vastapaneel.add(nullinupp);
			nullinupp.addActionListener(this);
		}
		vasta = new JButton("VASTA!");
		vasta.addActionListener(this);
		vastapaneel.add(vasta);
		this.add(vastapaneel, BorderLayout.EAST);
		j2rgmine = new JButton("J2rgmine!");
		j2rgmine.addActionListener(this);
		this.add(j2rgmine, BorderLayout.SOUTH);
		j2rgmine.setEnabled(false);
	}
		
	public void lae_skoori_paneel() {
		kysvast = new JPanel();
		kysvast.setLayout(new GridLayout(2, 1));
		protsentarv = 0;
		kysvastlabel = new JLabel(oiged + " / " + kysimustearv);
		kysvastlabel.setBackground(new Color(255, 222, 173));
		kysvastlabel.setOpaque(true);
		protsent = new JLabel(protsentarv + " %");
		protsent.setBackground(new Color(255, 222, 173));
		protsent.setOpaque(true);
		kysvast.add(kysvastlabel);
		kysvast.add(protsent);
		this.add(kysvast, BorderLayout.WEST);
		kysvast.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "VASTA!") {
			kysimustearv += 1;
			vasta.setEnabled(false);
			j2rgmine.setEnabled(true);
			boolean kasutaja_kyik_yigesti = true;
			int yigesti_vastatud_lugeja = 0;
			boolean vastas_ainult_yigetele = true;
			
			for (int i = 0; i < selectedQuestion.getHowManyAnswers(); i++) {
				if (tsekboxid.get(selectedQuestion.getHowManyAnswers()).get(i).isSelected()) {
					if (selectedQuestion.get_vastused().get(i).getVastus_kasoige()) 
						// kasutaja vastas selle konkreetse vastuse 6igesti
						yigesti_vastatud_lugeja++;
					else {
						vastas_ainult_yigetele= false;
						kasutaja_kyik_yigesti = false;
					}
				} else {
					if (selectedQuestion.get_vastused().get(i).getVastus_kasoige()) 
						kasutaja_kyik_yigesti = false;
				}
			} // end for
			
			double vastuse_eest_punkt=0;
			String ette = "";
			
			if (selectedQuestion.yigeid_vastuseid()== 0) {
				if (kasutaja_kyik_yigesti) {
					vastuse_eest_punkt = 1;
					ette = "Punkt " + vastuse_eest_punkt * 100 + " %  ";
					vastuse_protsent =  vastuse_eest_punkt * 100;
				} else {
					vastuse_eest_punkt = 0;
					ette = "Punkt " + 0 + " %  ";
					vastuse_protsent = 0;
				}
			}
			else  {
				if (!kasutaja_kyik_yigesti) {
					if (selectedQuestion.yigeid_vastuseid()> 1) {
							if (vastas_ainult_yigetele) {
								vastuse_eest_punkt = (double) yigesti_vastatud_lugeja / (double) selectedQuestion.yigeid_vastuseid();
								ette = "Punkt " + vastuse_eest_punkt * 100 + " %  ";
								vastuse_protsent =  vastuse_eest_punkt * 100;
							} 
							else {
								vastuse_eest_punkt = 0;
								ette = "Punkt " + 0 + " %  ";
								vastuse_protsent =  0;
							}
					} else {
						vastuse_eest_punkt = 0;
						ette = "Punkt " + 0 + " %  ";
						vastuse_protsent =  0;
					}					
				} else {
					vastuse_eest_punkt = yigesti_vastatud_lugeja / selectedQuestion.yigeid_vastuseid();
					ette = "Punkt " + vastuse_eest_punkt * 100 + " %  ";
					vastuse_protsent =  vastuse_eest_punkt * 100;
				}
			}
			
			if (kasutaja_kyik_yigesti) {
				j2rgmine.setText(ette + "6ige, j2rgmine!");
				oiged = oiged + vastuse_eest_punkt;
			} else {
				if (vastuse_eest_punkt > 0) {
					ette = ette + "Osaliselt ";
				} 
				j2rgmine.setText(ette + "Vale :(  Kliki siia, et minna edasi");
				oiged = oiged + vastuse_eest_punkt;
			}
			for (int i = 0; i < selectedQuestion.getHowManyAnswers(); i++) {
				leibelid.get(selectedQuestion.getHowManyAnswers()).get(i).setBackground(new Color(255, 200, 200));
			}
			for (int i = 0; i < selectedQuestion.getHowManyAnswers(); i++) {
				if (selectedQuestion.get_vastused().get(i).getVastus_kasoige()) {
					leibelid.get(selectedQuestion.getHowManyAnswers()).get(i).setBackground(new Color(200, 255, 200));
				}
			}
			try {
				protsentarv = (int) (oiged * 100 / kysimustearv);
			} catch (ArithmeticException o) {
				protsentarv = 0;
			}
			kysvastlabel.setText(oiged + " / " + kysimustearv);
			protsent.setText(protsentarv + " %");
		}
		
		if (e.getActionCommand().contains("6ige, j2rgmine!") || e.getActionCommand().contains( "Vale :(  Kliki siia, et minna edasi")) {
			j2rgmine.setText("J2rgmine!");
			count = 0;
			if (!this.gamemode) {					
					fortsykkel(); 
			}
		}
		
		if (e.getActionCommand() == "Nulli seis!") {
			oiged = 0;
			kysimustearv = 0;
			kysvastlabel.setText(oiged + " / " + kysimustearv);
			protsentarv = 0;
			protsent.setText(protsentarv + " %");
		}
	}
	
	public void fortsykkel()  {

		if (!menyyNupudTehtud) {
			lae_kysimuselabel();
			lae_vastustepaneel();
			tekita_listid();
			lae_nupud();
			lae_skoori_paneel();
		}

		menyyNupudTehtud = true;
		selectedQuestion = kysimuste_database.getNextSuvaKyss();
		selectedQuestion.randomizeAnswers();
		kyspikkus = (selectedQuestion.get_kysimus().length());

		/*
		pildiNupp = new ArrayList<JLabel>();
		if (!selectedQuestion.getKysimuse_pilt().isEmpty()) 
			if (!gamemode) {
				pildiNupp.add(new JLabel(annaIkoon("images/"+ selectedQuestion.getKysimuse_pilt())));
				kysimus.add(pildiNupp.get(pildiNupp.size() - 1));
			}
		*/
		
		kysimus.setText("<html>" + selectedQuestion.get_kysimus() + "</html>");
		kysimus.setFont(new Font("Arial",1,14));

		for (int i = 0; i < selectedQuestion.getHowManyAnswers(); i++) {
			String kysimus = "<html>" + selectedQuestion.get_vastused().get(i).getVastuse_text() + "</html>";
			leibelid.get(selectedQuestion.getHowManyAnswers()).get(i).setText(kysimus);
			leibelid.get(selectedQuestion.getHowManyAnswers()).get(i).setBackground(new Color(209, 238, 238));
			tsekboxid.get(selectedQuestion.getHowManyAnswers()).get(i).setSelected(false);
		}
		
		CardLayout cl = (CardLayout)(vastustepaneel.getLayout());
	    cl.show(vastustepaneel, "Kogum" + selectedQuestion.getHowManyAnswers());
	    // cl.show(vastustepaneel, "Kogum5");	
		// vastustepaneel.revalidate();
		
		vasta.setEnabled(true);
		j2rgmine.setEnabled(false);
		this.revalidate();
		this.repaint();
	}
}
